
using System;
using Publicuse.DBCore.Redis.ServiceStack;
using Publicuse.DBCore.Mongodb;
using Publicuse.DBCore.MySql;
using Publicuse.DBCore.Oracle;
using Publicuse.DBCore.SqlLite;
using Publicuse.Util;
using Publicuse.Entity;
using Publicuse.Util.Redis;

namespace Publicuse.DBCore
{
    public class Property : System.Attribute
    {
        public string Value { get; set; }

        public Property(string Value)
        {
            this.Value = Value;
        }
    }

    /// <summary>
    /// 数据库配置
    /// </summary>
    public class DbConfig
    {
        /// <summary>
        /// 从配置文件初始化数据库连接串
        /// </summary>
        /// <param name="conf"></param>
        /// <returns></returns>
        public static bool InitMySqlDBConfig(string dbstrwrite, string dbstrread, int dbIndex)
        {
            dbstrwrite = AesEncryptionUtil.AESDecrypt(dbstrwrite, out var msg);
            dbstrread = AesEncryptionUtil.AESDecrypt(dbstrread, out var msgb);
            var constr = dbstrwrite + "|" + dbstrread;
            DBHelperUtil<MySqlInstance>.GetDbHelper.AddInstance(constr, dbIndex);
            return true;
        }

        public static bool InitMySqlDapperDBConfig(string dbstrwrite, string dbstrread, int dbIndex)
        {
            dbstrwrite = AesEncryptionUtil.AESDecrypt(dbstrwrite, out var msg);
            dbstrread = AesEncryptionUtil.AESDecrypt(dbstrread, out var msgb);
            var constr = dbstrwrite + "|" + dbstrread;
            DBHelperUtil<MySqlDapperInstance>.GetDbHelper.AddInstance(constr, dbIndex);
            return true;
        }

        public static bool InitMongoDBConfig(string dbstr, int dbIndex)
        {
            var constr = AesEncryptionUtil.AESDecrypt(dbstr, out var msg);
            DBHelperUtil<MongodbInstance>.GetDbHelper.AddInstance(constr, dbIndex);
            return true;
        }

        public static bool InitRedisDBConfig(string dbstr)
        {
            var constr = AesEncryptionUtil.AESDecrypt(dbstr, out var msg);
            var server = constr.Split(',')[0];
            var pass = constr.Split(',')[1];
            ServiceStackProvider.AddInstance((int)RedisDBType.DB6376, new ServiceStackInstance(server, (int)RedisDBType.DB6376, pass));
            return true;
        }

        public static bool InitOracleDBConfig(string dbstr, int dbIndex)
        {
            var constr = AesEncryptionUtil.AESDecrypt(dbstr, out var msg);
            DBHelperUtil<MyOracleInstance>.GetDbHelper.AddInstance(constr, dbIndex);
            return true;
        }

        public static bool InitSqlLiteDBConfig(string  dbstr)
        {
            DBHelperUtil<SqlLiteInstance>.GetDbHelper.AddInstance(dbstr, 1);
            return true;
        }
    }
}
